﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Lib
{
    public class SingleInstance<T>
        where T : class,new()
    {
        private static Dictionary<Type, object> _single;
        public static T GetOne()
        {
            if (_single == null)
            {
                _single = new Dictionary<Type, object>();
            }

            T t = null;
            if (_single.ContainsKey(typeof(T)))
            {
                t = _single[typeof(T)] as T;
                if (t == null)
                {
                    _single.Remove(typeof(T));
                }
            }

            if (t == null)
            {
                t = new T();
                _single.Add(typeof(T), t);
            }

            return t;
        }
        public static void NewOne()
        {
            if (_single == null)
            {
                _single = new Dictionary<Type, object>();
            }

            if (_single.ContainsKey(typeof(T)))
            {
                _single.Remove(typeof(T));
            }

            T t = new T();
            _single.Add(typeof(T), t);
        }
    }
}
